from datetime import datetime
import sqlite3

class Post:
	author = content = permalink = title = u""
	commentcount= 0
	published = datetime(1970,1,1)	
	comments = tags = []		
		
class Comment:
	title = author = authorlink = content = permalink = u""
	published = datetime(1970,1,1)	
	
class Blog():
		
	def __init__(self, filepath):
		
		createPostsSQL = r'CREATE TABLE Posts (Permalink, Title, Author, Content, Published, Tags, CommentCount)'
		createCommentsSQL = r'CREATE TABLE Comments (PostPermalink, Permalink, Title, Author, AuthorLink, Content, Published)'
		
		self.connection = sqlite3.connect(filepath)
		
		c = self.connection.cursor()
		c.execute(createPostsSQL)
		c.execute(createCommentsSQL)				
		
		
	def AddPost(self, Post):
		try:
			c = self.connection.cursor()

			c.execute('INSERT INTO Posts VALUES(?, ?, ?, ?, ?, ?, ?)',
			         (Post.permalink, Post.title, Post.author, Post.content.strip(), Post.published.isoformat(), ','.join(Post.tags), Post.commentcount))
					
			for Comment in Post.comments:
				c.execute('INSERT INTO Comments VALUES(?, ?, ?, ?, ?, ?, ?)',
						  (Post.permalink, Comment.permalink, Comment.title, Comment.author, Comment.authorlink, Comment.content.strip(), Comment.published.isoformat()))

			self.connection.commit()

		finally:
			c.close()
			
	
		
